<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper" v-hasPermi="['platform:region:add']">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="地区名称">
                <a-input v-model="queryParam.name" placeholder="请输入地区名称" allow-clear @keyup.enter.native="handleQuery" />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="地区编码">
                <a-input v-model="queryParam.code" placeholder="请输入地区编码" allow-clear @keyup.enter.native="handleQuery" />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="地区简称">
                <a-input v-model="queryParam.abbreviation" placeholder="请输入地区简称" allow-clear @keyup.enter.native="handleQuery" />
              </a-form-item>
            </a-col>
            <a-col :span="6" v-if="advanced">
              <a-form-item label="地区类型">
                <a-select placeholder="请选择地区类型" v-model="queryParam.type" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in typeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6" v-if="advanced">
              <a-form-item label="私有类型">
                <a-select placeholder="请选择私有类型" v-model="queryParam.isPrivate" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in isPrivateOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <region-add-form v-if="showAddModal" ref="regionAddForm" :typeOptions="typeOptions" :isPrivateOptions="isPrivateOptions" @ok="getList" @close="showAddModal = false" />
      <!-- 编辑 -->
      <region-edit-form v-if="showEditModal" ref="regionEditForm" :typeOptions="typeOptions" :isPrivateOptions="isPrivateOptions" @ok="getList" @close="showEditModal = false" />
      <!-- 地图增加 -->
      <region-map-add-form v-if="showMapAddModal" ref="regionMapAddForm" @ok="getList" @close="showMapAddModal = false" :regionId="regionId" />
      <!-- 地图编辑 -->
      <region-map-edit-form v-if="showMapEditModal" ref="regionMapEditForm" @ok="getList" @close="showMapEditModal = false" :regionId="regionId" />
      <!-- 审核配置 -->
      <approve-config-table-modal v-if="showApproveConfigTableModal" ref="approveConfigTableModal" @ok="getList" @close="showApproveConfigTableModal = false" :itemId="regionId" />
      <!-- 审核记录 -->
      <approve-record-table-modal v-if="showApproveRecordTableModal" ref="approveRecordTableModal" @ok="getList" @close="showApproveRecordTableModal = false" :itemId="regionId" :statusOptions="statusOptions" />
      <advance-table
        title="地区数据"
        @expand="onExpandCurrent"
        :expandedRowKeys="expandedKeys"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-region-index-table"
        @change="handleTableChange"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="regionList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <div class="table-operations" slot="button">
          <a-button type="primary" @click="handleAdd" v-hasPermi="['platform:region:add']"> <a-icon type="plus" />新增 </a-button>
          <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete" v-hasPermi="['platform:region:remove']"> <a-icon type="delete" />删除 </a-button>
          <a-button type="" @click="handleExport" v-hasPermi="['platform:region:export']"> <a-icon type="download" />导出 </a-button>
        </div>
        <span slot="expandedRowRender" slot-scope="{ record }">
          <section-table ref="sectionTable" :title="record.dictName" :dictId="record.id" :dictType="record.dictType" :regionId="record.id" />
        </span>
        <span slot="avatar" slot-scope="{ record }">
          <a-avatar shape="square" :src="imageUrl + record.avatar" />
        </span>
        <span slot="province" slot-scope="{ record }">
          {{ getProvinceFormat(record) }}
        </span>
        <span slot="isPrivate" slot-scope="{ record }">
          {{ isPrivateFormat(record) }}
        </span>
        <span slot="type" slot-scope="{ record }">
          {{ typeFormat(record) }}
        </span>
        <span slot="status" slot-scope="{ record }">
          <a-badge :status="statusBage(record.status)" :text="statusFormat(record)" />
        </span>
        <span slot="operation" slot-scope="{ record }">
          <a @click="handleUpdate(record)" v-hasPermi="['platform:region:edit']">修改</a>
          <a-divider type="vertical" v-hasPermi="['platform:region:remove']" />
          <a @click="handleDelete(record)" v-hasPermi="['platform:region:remove']">删除</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多<a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleMap(record)">地图</a>
              </a-menu-item>
              <a-menu-item v-if="record.status === '2'">
                <a @click="handleApproveConfig(record)">审核配置</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleApproveRecord(record)">审核情况</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { listRegion, delRegion, exportRegion, getInitData } from '@/api/platform/region'
import { listRegionMap } from '@/api/platform/regionMap'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import RegionAddForm from '@/views/platform/region/modules/RegionAddForm'
import RegionEditForm from '@/views/platform/region/modules/RegionEditForm'
import RegionMapAddForm from '@/views/platform/regionmap/modules/RegionMapAddForm'
import RegionMapEditForm from '@/views/platform/regionmap/modules/RegionMapEditForm'
import SectionTable from '@views/platform/components/SectionTable'
import ApproveConfigTableModal from '@views/platform/components/ApproveConfigTableModal'
import ApproveRecordTableModal from '@views/platform/components/ApproveRecordTableModal'
import Area from '@/components/_util/Area'
export default {
  name: 'Region',
  components: {
    AdvanceTable,
    RegionAddForm,
    RegionEditForm,
    RegionMapAddForm,
    RegionMapEditForm,
    SectionTable,
    ApproveConfigTableModal,
    ApproveRecordTableModal
  },
  data() {
    return {
      //图片链接
      imageUrl: process.env.VUE_APP_BASE_API,
      showAddModal: false,
      showEditModal: false,
      showMapAddModal: false,
      showMapEditModal: false,
      showApproveConfigTableModal: false,
      showApproveRecordTableModal: false,
      regionId: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 地区表表格数据
      regionList: [],
      // 地区类型字典
      typeOptions: [],
      // 地区状态字典
      statusOptions: [],
      //  地区私有字典
      isPrivateOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code: undefined,
        abbreviation: undefined,
        type: undefined,
        isPrivate: undefined
      },
      expandedKeys: [],
      columns: [
        {
          title: '地区名称',
          dataIndex: 'name',
          ellipsis: true,
          align: 'left',
          width: '15%'
        },
        {
          title: '地区编码',
          dataIndex: 'code',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '地区头像',
          dataIndex: 'avatar',
          ellipsis: true,
          align: 'center',
          width: '15%',
          scopedSlots: { customRender: 'avatar' }
        },
        {
          title: '地区简称',
          dataIndex: 'abbreviation',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '地区类型',
          dataIndex: 'type',
          ellipsis: true,
          align: 'left',
          width: '10%',
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '地区省份',
          dataIndex: 'province',
          ellipsis: true,
          align: 'left',
          width: '10%',
          scopedSlots: { customRender: 'province' }
        },
        {
          title: '私有状态',
          dataIndex: 'isPrivate',
          ellipsis: true,
          align: 'left',
          width: '10%',
          scopedSlots: { customRender: 'isPrivate' }
        },
        {
          title: '状态',
          dataIndex: 'status',
          ellipsis: true,
          align: 'left',
          width: '10%',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '15%',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      pcaData: ''
    }
  },
  created() {
    this.pcaData = new Area()
    this.getList()
    getInitData('region_type,region_status,region_private').then(response => {
      this.typeOptions = response.data.region_type
      this.statusOptions = response.data.region_status
      this.isPrivateOptions = response.data.region_private
    })
  },
  methods: {
    /** 查询地区表列表 */
    getList() {
      this.loading = true
      listRegion(this.queryParam).then(response => {
        this.regionList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 地区类型字典翻译
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type)
      } else {
        return ''
      }
    },
    // 地区私有类型字典翻译
    isPrivateFormat(row) {
      if (row.isPrivate) {
        return this.selectDictLabel(this.isPrivateOptions, row.isPrivate)
      } else {
        return ''
      }
    },
    // 地区状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status)
      } else {
        return ''
      }
    },
    // 地区状态徽标数类型
    statusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'warning'
      } else if (status === '2') {
        return 'processing'
      } else if (status === '3') {
        return 'error'
      }
    },
    // 地区省份字典翻译
    getProvinceFormat(row) {
      if (row.province) {
        return this.pcaData.getText(row.province, 1)
      } else {
        return ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code: undefined,
        abbreviation: undefined,
        type: undefined,
        isPrivate: undefined
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleApproveRecord(record) {
      this.regionId = record.id
      this.showApproveRecordTableModal = true
      this.$nextTick(() => this.$refs.approveRecordTableModal.handleTable())
    },

    handleApproveConfig(record) {
      this.regionId = record.id
      this.showApproveConfigTableModal = true
      this.$nextTick(() => this.$refs.approveConfigTableModal.handleTable())
    },

    handleMap(record) {
      this.regionId = record.id
      listRegionMap({ regionId: this.regionId }).then(response => {
        if (response.data.rows.length > 0) {
          this.handleMapUpdate(response.data.rows[0], '')
        } else {
          this.handleMapAdd()
        }
      })
    },

    handleMapAdd() {
      this.showMapAddModal = true
      this.$nextTick(() => this.$refs.regionMapAddForm.handleAdd())
    },

    handleMapUpdate(record, ids) {
      this.showMapEditModal = true
      this.$nextTick(() => this.$refs.regionMapEditForm.handleUpdate(record, ids))
    },

    handleAdd() {
      this.showAddModal = true
      this.$nextTick(() => this.$refs.regionAddForm.handleAdd())
    },
    handleUpdate(record, ids) {
      this.showEditModal = true
      this.$nextTick(() => this.$refs.regionEditForm.handleUpdate(record, ids))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this
      const regionIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk() {
          return delRegion(regionIds).then(() => {
            that.onSelectChange([], [])
            that.getList()
            that.$message.success('删除成功', 3)
          })
        },
        onCancel() {}
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk() {
          return exportRegion(that.queryParam).then(response => {
            that.download(response.msg)
            that.$message.success('导出成功', 3)
          })
        },
        onCancel() {}
      })
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },
    onExpandCurrent(expandedKeys, row) {
      if (expandedKeys) {
        this.expandedKeys = [row.id]
      } else {
        this.expandedKeys = []
      }
    }
  }
}
</script>
